package gogogo.liwushuo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.BannerFirstActivity;
import gogogo.liwushuo.activity.HomeAuthorctivity;
import gogogo.liwushuo.activity.HomeImageActivity;
import gogogo.liwushuo.activity.HomeTitleActivity;
import gogogo.liwushuo.activity.FirstBannerActivity;
import gogogo.liwushuo.activity.SignInActivity;
import gogogo.liwushuo.adapter.RecyclerViewAdapter;
import gogogo.liwushuo.bean.HomeBean;
import gogogo.liwushuo.bean.HomeFirstViewBean;
import gogogo.liwushuo.bean.HomeSecondViewBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.listener.OnItemDigestClickListener;
import gogogo.liwushuo.listener.OnItemFvClickListener;
import gogogo.liwushuo.listener.OnItemIconClickListener;
import gogogo.liwushuo.listener.OnItemImageClickListener;
import gogogo.liwushuo.listener.OnItemLikeCountClickListener;
import gogogo.liwushuo.listener.OnItemLikeIconClickListener;
import gogogo.liwushuo.listener.OnItemLlayoutClickListener;
import gogogo.liwushuo.listener.OnItemNameClickListener;
import gogogo.liwushuo.listener.OnItemTitleClickListener;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * Created by chenjun on 2016/7/19.
 */
public class SubHomeFragment extends Fragment {

    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    private Activity mContext;
    private int index;
    private HomeBean commentBean;
    private List<HomeBean.DataBean.ItemsBean> itemsBeen;
    private RecyclerViewAdapter adapter;
    private LinearLayoutManager layout;
    private OKHttpUtils okHttpUtils;
    private String result1, result2;
    private HomeFirstViewBean firstbean;
    private HomeSecondViewBean secondbean;
    private List<HomeFirstViewBean.DataBean.BannersBean> firstbanners;
    private List<HomeSecondViewBean.DataBean.SecondaryBannersBean> secondbanners;
//    private OnFragmentDataGet fragmentDataGet;

    public static SubHomeFragment newInstance(int index) {
        Bundle b = new Bundle();
        b.putInt("index", index);
        SubHomeFragment fragment = new SubHomeFragment();
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Log.i("mtag", "onCreate: "+(mContext ==null));
        okHttpUtils = OKHttpUtils.newInstance(mContext);
        Bundle mData = getArguments();
        if (mData != null) {
            index = mData.getInt("index");
        }
        getJsonData();
    }

    public void getJsonData() {
        if (index == 0) {
            adapter = new RecyclerViewAdapter(mContext, index, itemsBeen, firstbanners, secondbanners);
        } else {
            adapter = new RecyclerViewAdapter(mContext, index, itemsBeen);
        }
        okHttpUtils.getAsyncMainData(Resource.URL_HOME[index], new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                commentBean = new Gson().fromJson(result, HomeBean.class);
                itemsBeen = commentBean.getData().getItems();
                if (index == 0) {
                    loadFirstScanner();
                } else {
                    adapter.setList(itemsBeen);
                }
                setListener();
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_recycler, container, false);
        ButterKnife.inject(this, view);
        setRecyclerVAdapter();
        return view;
    }

    private void setRecyclerVAdapter() {
        layout = new LinearLayoutManager(mContext);
        recyclerview.setLayoutManager(layout);
        recyclerview.setAdapter(adapter);
    }

    private void setListener() {
        adapter.setOnItemNameClickListener(new OnItemNameClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                if (index == 0) {
                    position--;
                }
                int id = itemsBeen.get(position).getAuthor().getId();
                Intent intent = new Intent(mContext, HomeAuthorctivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemIconClickListener(new OnItemIconClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv, RecyclerView.ViewHolder holder) {
                if (index == 0) {
                    position--;
                }
                int id = itemsBeen.get(position).getAuthor().getId();
                Intent intent = new Intent(mContext, HomeAuthorctivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemTitltClickListener(new OnItemTitleClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                if (index == 0) {
                    position--;
                }
                int id = itemsBeen.get(position).getColumn().getId();
                Intent intent = new Intent(mContext,HomeTitleActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("title",tv.getText());
                startActivity(intent);
            }
        });

        adapter.setOnItemLikeCountClickListener(new OnItemLikeCountClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv, TextView tv, RecyclerView.ViewHolder holder) {
                RecyclerViewAdapter.MyHolder myHolder = (RecyclerViewAdapter.MyHolder) holder;
                if (myHolder.isLike) {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_normal);
                    tv.setText((Integer.parseInt(tv.getText().toString()) - 1) + "");
                } else {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_selected);
                    tv.setText((Integer.parseInt(tv.getText().toString()) + 1) + "");
                }
                myHolder.isLike = !myHolder.isLike;
            }
        });
        adapter.setOnItemLikeIconClickListener(new OnItemLikeIconClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, ImageView iv,RecyclerView.ViewHolder holder) {
                RecyclerViewAdapter.MyHolder myHolder = (RecyclerViewAdapter.MyHolder) holder;
                if (myHolder.isLike) {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_normal);
                    tv.setText((Integer.parseInt(tv.getText().toString()) - 1) + "");
                } else {
                    iv.setImageResource(R.mipmap.ic_feed_favourite_selected);
                    tv.setText((Integer.parseInt(tv.getText().toString()) + 1) + "");
                }
                myHolder.isLike = !myHolder.isLike;
            }
        });
        adapter.setOnItemImageClickListener(new OnItemImageClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv,RecyclerView.ViewHolder holder) {
                if (index == 0) {
                    position--;
                }
                int id = itemsBeen.get(position).getId();
                Intent intent = new Intent(mContext,HomeImageActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        adapter.setOnItemDigestClickListener(new OnItemDigestClickListener() {
            @Override
            public void onItemClick(int position, TextView tv, RecyclerView.ViewHolder holder) {
                if (index == 0) {
                    position--;
                }
                int id = itemsBeen.get(position).getId();
                Intent intent = new Intent(mContext,HomeImageActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
    }

    private void loadFirstScanner() {
        okHttpUtils.getAsyncMainData(Resource.URL_FIRST_BANNERS, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
            }

            @Override
            public void onResponse(String result) {
                result1 = result;
                loadSecondaryView();
            }
        });
    }

    private void loadSecondaryView() {
        okHttpUtils.getAsyncMainData(Resource.URL_SECOND_BANNERS, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
            }

            @Override
            public void onResponse(String result) {
                result2 = result;
                initHead();
            }
        });
    }

    private void initHead() {
        firstbean = new Gson().fromJson(result1, HomeFirstViewBean.class);
        firstbanners = firstbean.getData().getBanners();
        secondbean = new Gson().fromJson(result2, HomeSecondViewBean.class);
        secondbanners = secondbean.getData().getSecondary_banners();

        adapter.setList(itemsBeen, firstbanners, secondbanners);
        adapter.setOnItemFvClickListener(new OnItemFvClickListener() {
            @Override
            public void onItemClick(int position, FlashView fv, RecyclerViewAdapter.FirstViewHolder holder) {
                int id = firstbanners.get(position).getTarget_id();
                String len = id+"";
                if (len.length() != 3){
                    Intent intent = new Intent(mContext,HomeImageActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(mContext,FirstBannerActivity.class);
//                    Log.i("mtag", "onItemClick: "+id);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }

            }
        });
        adapter.setOnItemLlayoutClickListener(new OnItemLlayoutClickListener() {
            @Override
            public void onItemClick(int position, ImageView iv, RecyclerViewAdapter.FirstViewHolder holder) {
              //position这个参数没有用
                Intent intent = new Intent();
                switch (iv.getId()){
                    case 0:
                        intent.setClass(mContext,BannerFirstActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",243);
                        startActivity(intent);
                        break;
                    case 2:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",246);
                        startActivity(intent);
                        break;
                    case 3:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",247);
                        startActivity(intent);
                        break;
                    case 4:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",244);
                        startActivity(intent);
                        break;
                    case 5:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",245);
                        startActivity(intent);
                        break;
                    case 6:
                        intent.setClass(mContext,FirstBannerActivity.class);
                        intent.putExtra("id",174);
                        startActivity(intent);
                        break;
                    case 7:
                        startActivity(new Intent(getActivity(), SignInActivity.class));
                }
            }
        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

//    public interface OnFragmentDataGet{
//        void getFragmentData(int i);
//    }
//
//    public void setOnFragmentDataGet(OnFragmentDataGet onFragmentDataGet){
//        this.fragmentDataGet = onFragmentDataGet;
//    }
}
